package com.vgmobile.cucrever2.product.list;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.vgmobile.cucrever2.R;
import com.vgmobile.cucrever2.api.ItemProductList;
import com.vgmobile.cucrever2.global.BaseActivity;

public class ListProductAdapter extends BaseAdapter{
	
	private ArrayList<ItemProductList> listItem;
	private LayoutInflater layoutInflater;
	private BaseActivity activity;
	private ArrayList<ImageView> listImageViews;
	
	public ListProductAdapter(BaseActivity activity, ArrayList<ItemProductList> listItem) {
		this.listItem = listItem;
		this.activity = activity;
		layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listImageViews = new ArrayList<ImageView>();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listItem.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public void clearMem(){
		for (int i = 0; i < listImageViews.size(); i++) {
			ImageView imageView = listImageViews.get(i);
			Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
			bitmap.recycle();
			bitmap = null;
			listItem.clear();
			Log.d("AAAAList", i + "  ");
		}
	}
	
	@Override
	public View getView(int position , View convertView, ViewGroup parent){
		View view = convertView;
        ViewHolder viewHolder = null;
        if (convertView == null) {
        	view = layoutInflater.inflate(R.layout.product_list_list_item, null);
        	viewHolder = new ViewHolder();
        	viewHolder.title = (TextView) view.findViewById(R.id.product_list_list_item_title);
        	viewHolder.price = (TextView) view.findViewById(R.id.product_list_list_item_price);
        	viewHolder.store = (TextView) view.findViewById(R.id.product_list_list_item_store);
        	viewHolder.location = (TextView) view.findViewById(R.id.product_list_list_item_location);
        	viewHolder.imageView = (ImageView) view.findViewById(R.id.product_list_list_item_image);
        	viewHolder.ratingBar = (RatingBar) view.findViewById(R.id.product_list_list_item_ratingbar);
        	view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if(!listImageViews.contains(viewHolder.imageView)){
        	listImageViews.add(viewHolder.imageView);
        }
    	viewHolder.title.setText(listItem.get(position).getTitle());
    	viewHolder.price.setText(listItem.get(position).getPrice());
    	viewHolder.store.setText(listItem.get(position).getStore());
    	viewHolder.location.setText(listItem.get(position).getLocation());
    	viewHolder.ratingBar.setProgress(listItem.get(position).getRating());
    	activity.loadImage(viewHolder.imageView, listItem.get(position).getImage());

    	return view;
	}
	
	class ViewHolder{
		TextView title, store, location, price;
		ImageView imageView;
		RatingBar ratingBar;
	}
}
